<template>
  <el-dialog title="提现申请" :visible.sync="visible" :close-on-click-modal="false" :before-close="close" append-to-body width="500px">
    <el-form ref="main-form" status-icon :model="mainQuery" :rules="rules" label-width="180px" label-position="left" :inline="false">
      <template v-if="data">
        <el-form-item label="销售员" size="mini" prop="">
          <span>{{data.salesName}}</span>
        </el-form-item>
        <el-form-item label="申请周期" size="mini" prop="">
          <span>{{data.statYear}}年{{data.statMonth}}月</span>
        </el-form-item>
        <el-form-item label="提现金额" size="mini" prop="">
          <span>{{ data.settleCurrency }} {{data.commission / 100}}</span>
        </el-form-item>
        <el-divider></el-divider>
        <!--  -->
        <el-form-item label="转账类型 " size="mini" prop="transType">
          <el-radio-group v-model="mainQuery.transType" @change="fetchCurrencyOptions">
            <el-radio label="1">银行卡</el-radio>
            <el-radio label="2">平台商户号</el-radio>
          </el-radio-group>
        </el-form-item>
        <!--  -->
        <el-form-item label="商户开户邮箱: " v-if="mainQuery.transType == 2" size="mini" prop="mchEmail">
          <el-input placeholder="" style="margin-bottom:10px;" v-model="mainQuery.mchEmail" readonly></el-input>
        </el-form-item>
        <!--  -->
        <template v-if="mainQuery.transType == 1">
          <el-form-item label="公司开户行名称 " size="mini" prop="bankAccountName">
            <el-input placeholder="" v-model="mainQuery.bankAccountName" @keyup.enter.native="submit" clearable></el-input>
          </el-form-item>
          <el-form-item label="开户张账号： " size="mini" prop="bankAccountNo">
            <el-input placeholder="" v-model="mainQuery.bankAccountNo" @keyup.enter.native="submit" clearable></el-input>
          </el-form-item>
          <el-form-item label="开户行行号: " size="mini" prop="bankOpenBankNo">
            <el-input placeholder="" v-model="mainQuery.bankOpenBankNo" @keyup.enter.native="submit" clearable></el-input>
          </el-form-item>
        </template>
        <!--  -->
        <el-form-item label="转账币种: " size="mini" prop="transCurrency">
          <el-select style="width: 150px;" v-model="mainQuery.transCurrency" placeholder="转账币种" :disabled="mainQuery.transType == 2" clearable>
            <el-option v-for="item in currencyOptions" :key="item.value" :label="item.value" :value="item.value">
            </el-option>
          </el-select>
          <span style="margin-left: 5px;">汇率：{{ rateCustomized }}</span>
        </el-form-item>
        <el-form-item label="Google 验证码 : " size="mini" prop="code">
          <el-input placeholder="" v-model="mainQuery.code" @keyup.enter.native="submit" clearable></el-input>
        </el-form-item>
      </template>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="close" size="mini">关 闭</el-button>
      <el-button @click="submit" type="primary" size="mini">确认提交</el-button>
    </div>
  </el-dialog>
</template>

<script>
import ApiMain from "@/api/commission.salesTrans";

export default {
  mixins: [],
  components: {},
  data() {
    return {
      visible: false,
      loading: false,
      isValid: -1,
      mainQueryRaw: null,
      mainQuery: {
        commissionId: '',
        transType: '2',
        code: '',
        transAmount: '',
        transCurrency: '',
        mchEmail: '',
        bankAccountName: '',
        bankAccountNo: '',
        bankOpenBankNo: '',
      },

      currencyOptions: [
      ],

      rules: {
        code: [{ required: true, message: "请填写验证码", trigger: "blur" }],
        transType: [{ required: true, message: "请选择转账方式", trigger: "blur" }],
        mchEmail: [{ required: true, message: "请填写要转帐邮箱", trigger: "blur" }],
        bankAccountName: [{ required: true, message: "请输入银行户头", trigger: "blur" }],
        bankAccountNo: [{ required: true, message: "请输入银行账号", trigger: "blur" }],
        bankOpenBankNo: [{ required: true, message: "请输入开户行行号", trigger: "blur" }],
      },
      id: null,
      data: {},
      activeName: "create",
      rateCustomized: 1,
    };
  },

  watch: {
    'mainQuery.transCurrency' (newVal) {
      this.handleTransCurrencyChange(newVal);
    }
  },

  mounted() {
    this.mainQueryRaw = Object.copy(this.mainQuery);
  },

  methods: {

    show(data) {
      this.data = data
      this.mainQuery.commissionId = data.id;
      this.fetchCurrencyOptions();
      this.visible = true;
    },

    async fetchCurrencyOptions() {
      let re = await ApiMain.getCurrencyOptions({ sourceCurrency: this.data.settleCurrency, transType: this.mainQuery.transType, salesId: this.data.salesId });
      this.currencyOptions = re.currency;
      if (this.currencyOptions.length > 0) {
        this.mainQuery.transCurrency = this.currencyOptions[0].value;
      }
      if (this.mainQuery.transType == 2) {
        this.mainQuery.mchEmail = re.mchEmail; 
      }
      this.handleTransCurrencyChange(this.mainQuery.transCurrency);
    },

    handleTransCurrencyChange (newVal) {
      for (let i in this.currencyOptions) {
        if (this.currencyOptions[i].value == newVal) {
          this.rateCustomized = this.currencyOptions[i].rateCustomized / 10000;
        }
      }
    },

    close() {
      this.visible = false;
      this.$emit("dialog-close");
      this.reset()
    },

    verifyForm() {
      var bool = true;
      this.$refs['main-form'].validate((valid) => bool = valid);
      return bool;
    },

    async submit() {
      if (!this.verifyForm()) return false;
      this.loading = true;
      let form = JSON.parse(JSON.stringify(this.mainQuery))
      let data = await ApiMain.add(form)
      this.$message({ message: '提现发起成功！', type: 'success' });
      this.loading = false;
      this.close()
    },

    reset() {
      this.$refs["main-form"] && this.$refs["main-form"].resetFields();
      this.id = null;
      this.data = null;
      this.mainQuery = {
        type: 2,
        commissionId: '',
        transType: '2',
        code: '',
        transAmount: '',
        transCurrency: '',
        mchEmail: '',
        bankAccountName: '',
        bankAccountNo: '',
        bankOpenBankNo: '',
      }
      this.activeName = "create";
    },
  },
};
</script>

<style lang="scss" scoped>
.wire {
  height: 1px;
  width: 100%;
  background-color: black;
  margin: 20px 0px;
}
.tipsSuccess {
  color: #3ab54a;
}
.tipsCatch {
  color: red;
}
</style>
